<?php
function addRows($adat, $oKod, $phpExcel, $elteres = false){
	foreach($adat['riportsorok'] as $sor){
		$params = json_decode(unserialize($sor['Riportsor']['params']));
		if(isset($params->osszesito)){
			$style = array(
							'font' => array('bold' => true),
							'fill' => array(
											'type' => PHPExcel_Style_Fill::FILL_SOLID,
											'startcolor' => array('argb' => 'EEEEEEEE')
											)
							);
		}
		else{
			$style = array();
		}
		$osszes = 0;
		$row = array($sor['Riportsor']['megnevezes']);
		foreach($adat['honapok'] as $ho){
			$osszes += $row[] = $adat['beszamolo'][$oKod][$sor['Riportsor']['megnevezes']][$ho];
		}
		//Összesent vagy tény-tervet számítunk?
		$osszes = $elteres ? ($adat['beszamolo'][$oKod][$sor['Riportsor']['megnevezes']][2] -  $adat['beszamolo'][$oKod][$sor['Riportsor']['megnevezes']][1]) : $osszes;
		$row[] = $osszes;
		$this->PhpExcel->addTableRow($row, $style);
	}
}

$this->PhpExcelRrd->createWorksheet();
$this->PhpExcelRrd->xls->getDefaultStyle()->getNumberFormat()->setFormatCode('# ### ### ##0');

//osztályonként ezek a sheetek kellenek: terv, tény, tény-terv

$sheetIndex = -1;
$honap = array('január', 'február', 'március', 'április', 'május', 'június', 'július', 'augusztus', 'szeptember', 'október', 'november', 'december');

foreach($teny['osztalyok'] as $oKod => $osztaly){

	$table = array(
		array('label' => 'Megnevezés', 'width' => 25)
	);
	foreach($teny['honapok'] as $ho){
		$table[] = array('label' => $honap[($ho-1)], 'width' => 10);
	}
	$table[] = array('label' => 'Összesen', 'width' => 10);

	if(isset($terv['osztalyok'][$oKod])){
		//terv
		if($sheetIndex != -1){
			$this->PhpExcelRrd->xls->createSheet();
		}
		$this->PhpExcelRrd->xls->setActiveSheetIndex(++$sheetIndex)
				->setTitle($osztaly['nev'].'Terv')
				->setCellValue('A1', $oKod . ' ' . $osztaly['nev'])
				->setCellValue('C1', 'Terv');
		$this->PhpExcelRrd->xls->getActiveSheet()->getStyle("A1:C1")->applyFromArray(array("font" => array("bold" => true)));
		$this->PhpExcelRrd->xls->getActiveSheet()->getStyle("A1:C1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$this->PhpExcelRrd->setRow(3);
		$this->PhpExcelRrd->addTableHeader($table, array('bold' => true, 'h_align' => 'center'));
		addRows($terv, $oKod, $this->PhpExcelRrd);
		$this->PhpExcelRrd->addTableFooter();
	}

	if(isset($teny['osztalyok'][$oKod])){
		$this->PhpExcelRrd->xls->createSheet();
		//Osztály id, név, tény
		$this->PhpExcelRrd->xls->setActiveSheetIndex(++$sheetIndex)
				->setTitle($osztaly['nev'].'Teny')
				->setCellValue('A1', $oKod . ' ' . $osztaly['nev'])
				->setCellValue('C1', 'Tény');
		$this->PhpExcelRrd->xls->getActiveSheet()->getStyle("A1:C1")->applyFromArray(array("font" => array("bold" => true)));
		$this->PhpExcelRrd->xls->getActiveSheet()->getStyle("A1:C1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	
		// heading
		$this->PhpExcelRrd->setRow(3);
		$this->PhpExcelRrd->addTableHeader($table, array('bold' => true, 'h_align' => 'center'));
		addRows($teny, $oKod, $this->PhpExcelRrd);
		$this->PhpExcelRrd->addTableFooter();
	}
	
	if(isset($teny['osztalyok'][$oKod]) && isset($terv['osztalyok'][$oKod])){
		//tény-terv
		$this->PhpExcelRrd->xls->createSheet();
		$this->PhpExcelRrd->xls->setActiveSheetIndex(++$sheetIndex);
		$this->PhpExcelRrd->xls->getActiveSheet()->setTitle($osztaly['nev'].'Tény-Terv');
		$this->PhpExcelRrd->xls
				->setActiveSheetIndex($sheetIndex)
				->setCellValue('A1', $oKod . ' ' . $osztaly['nev'])
				->setCellValue('C1', 'Tény-Terv');	
		$this->PhpExcelRrd->xls->getActiveSheet()->getStyle("A1:C1")->applyFromArray(array("font" => array("bold" => true)));
		$this->PhpExcelRrd->xls->getActiveSheet()->getStyle("A1:C1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$table = array(
			array('label' => 'Megnevezés', 'width' => 25),
			array('label' => 'Terv', 'width' => 10),
			array('label' => 'Tény', 'width' => 10),
			array('label' => 'Eltérés', 'width' => 10)
			);
		$this->PhpExcelRrd->setRow(3);
		$this->PhpExcelRrd->addTableHeader($table, array('bold' => true, 'h_align' => 'center'));
		$osztalyok = $teny['osztalyok'] + $terv['osztalyok'];
		foreach($teny['riportsorok'] as $r){
			foreach($osztalyok as $osztalyKod => $o){
				foreach($teny['honapok'] as $ho){
					$tenyterv['beszamolo'][$osztalyKod][$r['Riportsor']['megnevezes']][1] += $terv['beszamolo'][$osztalyKod][$r['Riportsor']['megnevezes']][$ho];
					$tenyterv['beszamolo'][$osztalyKod][$r['Riportsor']['megnevezes']][2] += $teny['beszamolo'][$osztalyKod][$r['Riportsor']['megnevezes']][$ho];
				}
			}
		}
		$tenyterv['riportsorok'] = $teny['riportsorok'];
		$tenyterv['honapok'] = array(1, 2);	//1: terv, 2: tény
		addRows($tenyterv, $oKod, $this->PhpExcelRrd, true);
		$this->PhpExcelRrd->addTableFooter();
	}
}
$this->PhpExcelRrd->xls->setActiveSheetIndex(0);
$this->PhpExcelRrd->output();
?>